\section{Object Tracking}
\label{sec:ObjectTracking}
In this section, the design of the actual object tracking is given using the previous Controller and Webcam Readout results.

\subsection{Object Recognition}
In order to track objects, objects need to be recognized within each frame coming from the webcam. The object recognition is implemented on the raw yuv buffer that has the YUY2 format\citep{YUVYUY2}. The YUY2 Y, U and V channels are given in figure~\ref{fig:YUY2}. In the YUV video format, the Y represents the brightness or luminance and U and V represent the chrominance with respectively blue and red corrected for the brightness.

\begin{figure}[h!]
	\centering
	\includegraphics[width=0.8\textwidth]{images/yuy2.png} 
	\caption{YUY2 byte definition \citep{YUVYUY2}}
	\label{fig:YUY2}
\end{figure}

\begin{figure}[h!]
	\centering
	\includegraphics[width=0.3\textwidth]{images/600px-YUV_UV_plane.png} 
	\caption{Example of U-V color plane, Y value = 0.5 \citep{YUVcolorplane}}
	\label{fig:YUVcolorplane}
\end{figure}

From figure~\ref{fig:YUVcolorplane} it can be seen that the color orange occurs when U is small and V is large. In the YUV buffer the luminance and chrominance are unsigned in contrast to figure~\ref{fig:YUVcolorplane} which uses values between -0.5 and 0.5. The object position is calculated by taking the average x and y positions for pixels obeying $U<90$ and $V>180$.

\subsection{Difference angle measurement}
\label{subsec:diffangle}
From the Object Recognition, the position of the object with respect to the current webcam position is given in pixels. From the specification of the webcam\citep{WEBCAMSPECS} it follows that the diagonal field of view equals 63$^{\circ}$, from there, the conversion of pixels to radians can be computed using the 160x120 resolution:

\begin{align}
\Delta \Phi_{vertical} &= \frac{pixels_{vertical}}{\sqrt{160^2+120^2}} \cdot \frac{63}{180} \cdot \pi \nonumber \\
					&\approx 5.5\cdot 10^{-3} \cdot pixels_{vertical} \, rad \label{eq:verticalPix2RadConv} \\
\Delta \Phi_{horizontal} &= \frac{pixels_{horizontal}}{\sqrt{160^2+120^2}} \cdot \frac{63}{180} \cdot \pi \nonumber \\
					&\approx 5.5\cdot 10^{-3} \cdot pixels_{horizontal} \, \label{eq:horizontalPix2RadConv} rad
\end{align}
The results of equations \eqref{eq:verticalPix2RadConv} and \eqref{eq:horizontalPix2RadConv} will be used in the 20-sim model derived in subsection~\ref{subseq:tracking20sim}.

Note that the webcam view angle imposes an absolute maximum on the angular motion that is traceable, for example the maximal horizontal movement:

\begin{align*}
\omega_{horizontal,max} &= \frac{1}{2} \frac{63}{180} \pi \,\,rad/\frac{1}{30}s \\
	&= 30 \cdot \frac{1}{2} \frac{63}{180} \pi \approx 16.5 \,rad/s
\end{align*}
Which corresponds to a maximum traceable linear velocity of $16.5\cdot2=33\,m/s\approx 120\,km/h$ for an object at 2 meter following a circular orbit. Note that the JIWY setup is likely to limit the maximum traceable velocity due to its system dynamics.

\subsection{20-sim Model}
\label{subseq:tracking20sim}
The object tracking that is performed on the Gumstix is in fact a second controller communicating with the 100Hz controller that has been shown in section~\ref{sec:Controller}. This second controller running on 30Hz\footnote{The second controller runs on 30Hz because of the 30fps setting. However, it is dependend on the total load on the Gumstix whether or not the 30fps can be served since the Gumstix linux version is non real-time.} is designed and simulated in 20-sim in order to decide on the control algorithms to be used (e.g. P, PD or PID controller).

The 20-sim model showing both the 30Hz webcam controller and the 100Hz NIOS control loop is given in figure~\ref{fig:gumstixAndControl}. In the left part of the figure, the moving object's angle is represented by sinusoid sources for both the x and y coordinates. From there, the webcam controller measures the difference angle between the current JIWY position and the object's position.

\begin{figure}[h!]
	\centering
	\includegraphics[width=0.9\textwidth]{images/complete_system_simulation.png} 
	\caption{20-sim JIWY design containing 30Hz image recognition and 100Hz control loop}
	\label{fig:gumstixAndControl}
\end{figure}

The internals of the webcam controller can be seen in figure~\ref{fig:webcamController}. From left to right, and AD converter, proportional controller, delay, summation block and DA converter can be seen. The AD / DA converters are being used to convert from the continuous time simulation to the 30Hz discrete time system. The proportional controller is set to 0.5 which must be seen in combination with the gain of the 100Hz controller, which is 5 and 2 for the horizontal and vertical PID controller, altogether, the gain of the system is large. Furthermore the discrete delay is added to model the delay in the webcam, gumstix and serial communication and the summation block Sigma sums all relative measured angles to the absolute angle used as the set-point for the 100Hz loop.

\begin{figure}[h!]
	\centering
	\includegraphics[width=0.7\textwidth]{images/complete_system_simulation_webcamcontroller.png} 
	\caption{Webcam controller internals}
	\label{fig:webcamController}
\end{figure}


\subsection{Testing}
The simulation results for both the horizontal and vertical position are depicted in figure~\ref{fig:webcamControllerResults}. From the simulation results it can be seen that the overall system tends towards instability as a consequence of the amount of gain whenever the difference between the measured object and the JIWY position is to large. Note that in reality this situation would never occur do to the limited view-angle of the webcam.

\begin{figure}[h!]
	\centering
	\includegraphics[width=\textwidth]{images/complete_system_simulation_results.png} 
	\caption{20-sim simulation results containing 30Hz image recognition loop and 100Hz control loop}
	\label{fig:webcamControllerResults}
\end{figure}